<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="两地三中心,异地多活,金融灾备" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="写在前面灾备的本质是冗余数据，而冗余的副作用就是带来数据一致性的问题。对于金融的业务场景，有些经典的强一致性场景。而这些强一致性场景就是金融灾备的难点所在。 经典场景1）余额扣减 2）库存扣减 这两个场景是灾备的经典场景，也是金融难点，使我们要解决的核心问题。 如果余额不能保证强一致性，就有可能出现超扣的情况，比如用户只有100元，却扣掉了200元，这样就凭空产生了100元，这个就是大问题了。库存">
<meta name="keywords" content="两地三中心,异地多活,金融灾备">
<meta property="og:type" content="article">
<meta property="og:title" content="金融灾备落地实践">
<meta property="og:url" content="http://www.zhizus.com/2017-09-21-金融灾备落地实践.html">
<meta property="og:site_name" content="郑大侠的博客">
<meta property="og:description" content="写在前面灾备的本质是冗余数据，而冗余的副作用就是带来数据一致性的问题。对于金融的业务场景，有些经典的强一致性场景。而这些强一致性场景就是金融灾备的难点所在。 经典场景1）余额扣减 2）库存扣减 这两个场景是灾备的经典场景，也是金融难点，使我们要解决的核心问题。 如果余额不能保证强一致性，就有可能出现超扣的情况，比如用户只有100元，却扣掉了200元，这样就凭空产生了100元，这个就是大问题了。库存">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://www.zhizus.com/images/same-city.png">
<meta property="og:image" content="http://www.zhizus.com/images/different-city.png">
<meta property="og:updated_time" content="2018-05-28T02:35:39.707Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="金融灾备落地实践">
<meta name="twitter:description" content="写在前面灾备的本质是冗余数据，而冗余的副作用就是带来数据一致性的问题。对于金融的业务场景，有些经典的强一致性场景。而这些强一致性场景就是金融灾备的难点所在。 经典场景1）余额扣减 2）库存扣减 这两个场景是灾备的经典场景，也是金融难点，使我们要解决的核心问题。 如果余额不能保证强一致性，就有可能出现超扣的情况，比如用户只有100元，却扣掉了200元，这样就凭空产生了100元，这个就是大问题了。库存">
<meta name="twitter:image" content="http://www.zhizus.com/images/same-city.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://www.zhizus.com/2017-09-21-金融灾备落地实践.html"/>





  <title> 金融灾备落地实践 | 郑大侠的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f75c9cec9ecead0dfce9a108b9688072";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">郑大侠的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-crossed">
          <a href="http://zhizus.com" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-camera-retro"></i> <br />
            
            穿越
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-灾备 " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.zhizus.com/2017-09-21-金融灾备落地实践.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="DempeZheng">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑大侠的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                金融灾备落地实践
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            

            

            
          </span>

          

          
            
          

          
          
             <span id="/2017-09-21-金融灾备落地实践.html" class="leancloud_visitors" data-flag-title="金融灾备落地实践">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h2><p>灾备的本质是冗余数据，而冗余的副作用就是带来数据一致性的问题。对于金融的业务场景，有些经典的强一致性场景。而这些强一致性场景就是金融灾备的难点所在。</p>
<h2 id="经典场景"><a href="#经典场景" class="headerlink" title="经典场景"></a>经典场景</h2><p>1）<strong>余额扣减</strong>
2）<strong>库存扣减</strong>
这两个场景是灾备的经典场景，也是金融难点，使我们要解决的核心问题。
如果余额不能保证强一致性，就有可能出现超扣的情况，比如用户只有100元，却扣掉了200元，这样就凭空产生了100元，这个就是大问题了。库存扣减同理。
而充值同样是金钱相关的重要业务，但是就没有上面的问题，充值的本质是给账户加钱，加钱不会产生负数，即便冗余了数据，只要最终同步了，就能保证数据最终一致。</p>
<h2 id="灾备的演变"><a href="#灾备的演变" class="headerlink" title="灾备的演变"></a>灾备的演变</h2><h3 id="同城“双活”"><a href="#同城“双活”" class="headerlink" title="同城“双活”"></a>同城“双活”</h3><p><img src="/images/same-city.png" alt="Alt text">
阿里早期的架构就是同城“<code>双活</code>”，而这个双活是业务层的双活，DB层还是单点写。机房B的数据库作为机房B的冷备，
故障的时候，将冷备机房的数据库切换为master。一般不经过多次演练，切换的风险还是比较高的。</p>
<h3 id="同城“双活”，异地只读"><a href="#同城“双活”，异地只读" class="headerlink" title="同城“双活”，异地只读"></a>同城“双活”，异地只读</h3><p><img src="/images/different-city.png" alt="Alt text"></p>
<h3 id="两地三中心"><a href="#两地三中心" class="headerlink" title="两地三中心"></a>两地三中心</h3><h3 id="异地多活"><a href="#异地多活" class="headerlink" title="异地多活"></a>异地多活</h3><h2 id="灾备实施要点"><a href="#灾备实施要点" class="headerlink" title="灾备实施要点"></a>灾备实施要点</h2><h3 id="数据同步"><a href="#数据同步" class="headerlink" title="数据同步"></a>数据同步</h3><p>1）mysql的binlog复制
2）otter同步</p>
<h4 id="数据同步监控"><a href="#数据同步监控" class="headerlink" title="数据同步监控"></a>数据同步监控</h4><h3 id="切换方案"><a href="#切换方案" class="headerlink" title="切换方案"></a>切换方案</h3><h3 id="切换演练"><a href="#切换演练" class="headerlink" title="切换演练"></a>切换演练</h3><h2 id="落地要点"><a href="#落地要点" class="headerlink" title="落地要点"></a>落地要点</h2><p>1）明确需求，结合实际情况，选取合适的架构，适合的才是最好的；
2）剥离边缘业务，关注重点，不要被边缘业务干扰；
3）切换演练</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>关于金融灾备一直都是说的多，落地的少；现在除了阿里一些大公司有实力做异地双活，大部分的灾备方案也就是冷备，即便落地了，碰到机房故障的，没几个敢切的，最重要的是冷备还浪费机器。
但是，即便是有这么多缺点，但是依然很多公司乐此不疲的推动灾备的事情，因为大家已经意识到这个问题的重要性，对于金融灾备本质上不是一个可选项，是必须解决的一个问题。
所以，最好的情况是灾备我们必须实施，同时尽可能的降低切换的可能。</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>大侠劫富济贫啦！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/dempe/20180527112524.jpg" alt="DempeZheng WeChat Pay"/>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017-09-12浮点数的精度.html" rel="next" title="浮点数的精度">
                <i class="fa fa-chevron-left"></i> 浮点数的精度
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017-10-31-mysql binlog还原sql.html" rel="prev" title="mysql binlog还原sql">
                mysql binlog还原sql <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://avatars3.githubusercontent.com/u/6263631?v=3&s=466"
               alt="DempeZheng" />
          <p class="site-author-name" itemprop="name">DempeZheng</p>
           
              <p class="site-description motion-element" itemprop="description">請更固執，將自己謀生與擅長的事情做到最好</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">68</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">23</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/dempeZheng" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://music.163.com/#/user/home?id=6758793" target="_blank" title="网易云音乐">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  网易云音乐
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/dempe" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.wolfbe.com/" title="朗度云" target="_blank">朗度云</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#写在前面"><span class="nav-number">1.</span> <span class="nav-text">写在前面</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#经典场景"><span class="nav-number">2.</span> <span class="nav-text">经典场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#灾备的演变"><span class="nav-number">3.</span> <span class="nav-text">灾备的演变</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#同城“双活”"><span class="nav-number">3.1.</span> <span class="nav-text">同城“双活”</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#同城“双活”，异地只读"><span class="nav-number">3.2.</span> <span class="nav-text">同城“双活”，异地只读</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#两地三中心"><span class="nav-number">3.3.</span> <span class="nav-text">两地三中心</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#异地多活"><span class="nav-number">3.4.</span> <span class="nav-text">异地多活</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#灾备实施要点"><span class="nav-number">4.</span> <span class="nav-text">灾备实施要点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据同步"><span class="nav-number">4.1.</span> <span class="nav-text">数据同步</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#数据同步监控"><span class="nav-number">4.1.1.</span> <span class="nav-text">数据同步监控</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换方案"><span class="nav-number">4.2.</span> <span class="nav-text">切换方案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换演练"><span class="nav-number">4.3.</span> <span class="nav-text">切换演练</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#落地要点"><span class="nav-number">5.</span> <span class="nav-text">落地要点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考资料"><span class="nav-number">7.</span> <span class="nav-text">参考资料</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">DempeZheng</span>
</div>



        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  






  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("dhzMp0HdJLWMCrt04bwybWzH-gzGzoHsz", "gH6FmxJiUaVLLRXQDWpkkIVv");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

</body>
</html>
